package com.ylsf.blog.service.impl;

import com.ylsf.blog.mapper.ArticleMapper;
import com.ylsf.blog.mapper.CommentMapper;
import com.ylsf.blog.pojo.Article;
import com.ylsf.blog.pojo.ArticleComment;
import com.ylsf.blog.pojo.Comment;
import com.ylsf.blog.service.DetailService;
import com.ylsf.blog.utils.SqlSessionUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * 文章详情服务实现类
 */
public class DetailServiceImpl implements DetailService {
    @Override
    public ArticleComment displayArticleComment(Integer id) {
        SqlSession sqlSession = SqlSessionUtil.openSession();
        ArticleMapper articleMapper = sqlSession.getMapper(ArticleMapper.class);
        CommentMapper commentMapper = sqlSession.getMapper(CommentMapper.class);
        Article article = articleMapper.selectById(id);
        List<Comment> comments = commentMapper.selectCommentsArticle(id);
        ArticleComment articleComment=new ArticleComment(article.getId(),article.getTitle(),article.getContent(),article.getPublishedTime(),article.getAuthor(),comments);
        articleComment.setComments(comments);
        sqlSession.commit();
        SqlSessionUtil.close(sqlSession);
        return articleComment;
    }
}
